package com.umeox.watch.moto.apiclient.http;


import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.umeox.watch.httpclient.core.exception.SdkException;
import com.umeox.watch.moto.apiclient.BuildConfig;
import com.umeox.watch.moto.apiclient.http.callback.WaasCallback;
import com.umeox.watch.moto.apiclient.http.callback.WaasJsonCallback;
import com.umeox.watch.moto.apiclient.http.client.APIClientImpl;
import com.umeox.watch.moto.apiclient.http.request.WaasRequest;
import com.umeox.watch.moto.apiclient.http.request.WaasRequestBuilder;
import com.umeox.watch.moto.apiclient.http.response.WaasResponse;
import com.umeox.watch.moto.apiclient.http.response.WaasResponseImpl;

import java.lang.reflect.Type;
import java.util.Map;

public abstract class BaseApi {
    public static final int NORMAL_API = 0;
    public static final int DEFINE_API = 1;
    public static final int FILE_API = 2;

    protected String clientID, clientSecret;

    protected APIClientImpl apiClient;

    protected String accessToken = "";

    public void init(String clientID, String clientSecret, String accessToken) {
        this.clientID = clientID;
        this.clientSecret = clientSecret;
        this.accessToken = accessToken;
        if (TextUtils.isEmpty(this.clientID) || TextUtils.isEmpty(this.clientSecret)) {
            throw new SdkException("app key or app secret must be initialed");
        }
        if (BuildConfig.DEBUG) {
            Log.i("BaseApi","CLIENT_ID:" + this.clientID + " ;CLIENT_SECRET:" + this.clientSecret);
        }
    }

    protected WaasResponse sendSyncRequest(ApiEnum apiInt, Map<String, Object> params) {
        final String host = getHost(apiInt);
        WaasRequest request = new WaasRequestBuilder()
                .setApi(apiInt)
                .setHost(host)
                .setAccessToken(apiInt != ApiEnum.ACCESS_TOKEN ? accessToken : null)
                .setParas(params).build();
        WaasResponseImpl response = (WaasResponseImpl) apiClient.send(request);
        if (response.isSuccessful()) {
            try {
                JSONObject result = (JSONObject) response.getData();
                if (result.containsKey("status")) {
                    Type t = request.getApi().getTypeToken().getType();
                    if (t != null) {
                        if (t.getClass().isAssignableFrom(String.class)) {
                            response.setData(result.getString("result"));
                        } else {
                            response.setData(JSON.parseObject(result.getString("result"), t));
                        }
                    }
                    response.setCode(result.getInteger("status"));
                    response.setMessage(result.getString("msg"));
                } else if (result.containsKey("code")) {
                    response.setCode(result.getInteger("code"));
                    response.setMessage(result.getString("message"));
                } else {
                    response.setCode(-1);
                    response.setMessage(result.toJSONString());
                }
            } catch (Exception e) {
                if (BuildConfig.DEBUG) {
                    e.printStackTrace();
                }
                response.setCode(-1);
                response.setMessage(BuildConfig.DEBUG ? "返回结果格式转换错误" : "服务器繁忙，请稍后试试！");
            }
        }
        return response;
    }

    protected void sendAsyncRequest(ApiEnum apiInt, Map<String, Object> params, WaasCallback callback) {
        WaasRequest request = new WaasRequestBuilder()
                .setApi(apiInt)
                .setHost(getHost(apiInt))
                .setAccessToken(apiInt != ApiEnum.ACCESS_TOKEN ? accessToken : null)
                .setParas(params).build();
        apiClient.send(request, new WaasJsonCallback(callback));
    }

    protected abstract String getHost(ApiEnum apiEnum);
}
